//
//  IAPHelper.h
//
//  Original Created by Ray Wenderlich on 2/28/11.
//  Created by zhuzj on 7/9/12.
//  Copyright 2011 Ray Wenderlich. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "StoreKit/StoreKit.h"
#import "YYKit.h"

typedef void (^IAPProductsResponseBlock)(SKProductsRequest* request , SKProductsResponse* response, NSError *error);

typedef void (^IAPbuyProductCompleteResponseBlock)(SKPaymentTransaction* transaction);

typedef void (^IAPToCheckReceiptBlock)(SKPaymentTransaction* transaction);

typedef void (^IAPCheckReceiptCompleteResponseBlock)(id response,NSError* error);

typedef void (^IAPResoreProductsCompleteResponseBlock) (SKPaymentQueue* payment,NSError* error);

typedef void (^IAPTransactionStateBlock)(SKPaymentTransaction* transaction);

@interface IAPHelper : NSObject <SKProductsRequestDelegate, SKPaymentTransactionObserver>

@property (nonatomic,strong) NSSet *productIdentifiers;
@property (nonatomic,strong) NSArray * products;
@property (nonatomic,strong) SKProductsRequest *request;
@property (nonatomic) BOOL production;//开发、生产环境

- (instancetype)init;
- (void)addTransactionObserver:(IAPTransactionStateBlock)transactionState
           toCheckReceiptBlock:(IAPToCheckReceiptBlock)toCheckReceiptBlock
                  onCompletion:(IAPbuyProductCompleteResponseBlock)completion;


///判断用户是否允许app内购
- (BOOL)canMakePayments;

//get Products List and init With Product Identifiers
- (void)requestProductsWithProductIdentifiers:(NSSet *)productIdentifiers
                                   completion:(IAPProductsResponseBlock)completion;

//Buy Product
- (void)buyProductWithSKProduct:(SKProduct *)product
               transactionState:(IAPTransactionStateBlock)transactionState
            toCheckReceiptBlock:(IAPToCheckReceiptBlock)toCheckReceiptBlock
                   onCompletion:(IAPbuyProductCompleteResponseBlock)completion;

//restore Products
- (void)restoreProductsTransactionState:(IAPTransactionStateBlock)transactionState
                             completion:(IAPResoreProductsCompleteResponseBlock)completion
                    toCheckReceiptBlock:(IAPToCheckReceiptBlock)toCheckReceiptBlock;

// finishTransaction (结束交易)
- (void)finishTransaction:(SKPaymentTransaction *)transaction;

//check receipt but recommend to use in server side instead of using this function
- (void)checkReceipt:(NSData*)receiptData
        onCompletion:(IAPCheckReceiptCompleteResponseBlock)completion;

- (void)checkReceipt:(NSData*)receiptData
     andSharedSecret:(NSString*)secretKey
        onCompletion:(IAPCheckReceiptCompleteResponseBlock)completion;

//Get The Price with local currency
- (NSString *)getLocalePrice:(SKProduct *)product;

@end
